package com.ruoyi.web.controller.demo.controller;

import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author hnb
 * @Date 2024/12/12
 * @Description 腾讯云短信
 **/
public class DescribeInstances {
    public static void main(String[] args) {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(4);
        integers.add(-9);
        integers.add(0);
        integers.add(45);
        integers.add(-9999);
        quickSort(integers);
        System.out.println("==="+integers);
//        aa();
    }

    public void cc() {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential("AKIDMbptoX3OXUEoBPRH0b3oOuIUXO5Z9j9P", "EMLCqW3So052CTdcVGtNjz50lOp5A6sV");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"17854217400"};
            req.setPhoneNumberSet(phoneNumberSet1);
            String templateId = "449739";
            req.setTemplateId(templateId);
            req.setSmsSdkAppId("1400936573");
            req.setSignName("高新国资运营公司");
            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(AbstractModel.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

    public static void aa() {
        try {

            Credential cred = new Credential("AKIDMbptoX3OXUEoBPRH0b3oOuIUXO5Z9j9P", "EMLCqW3So052CTdcVGtNjz50lOp5A6sV");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("GET"); // get请求(默认为post请求)
            httpProfile.setConnTimeout(10); // 请求连接超时时间，单位为秒(默认60秒)
            httpProfile.setWriteTimeout(10);  // 设置写入超时时间，单位为秒(默认0秒)
            httpProfile.setReadTimeout(10);  // 设置读取超时时间，单位为秒(默认0秒)
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            SendSmsRequest req = new SendSmsRequest();
            String sdkAppId = "1400936573";
            req.setSmsSdkAppId(sdkAppId);
            String signName = "山东海创投资控股";
            req.setSignName(signName);
//            String templateId = "2462822";
//            String templateId = "2480566";
            String templateId = "2486339";
            req.setTemplateId(templateId);
//            String[] templateParamSet = {"您好!您的OA系统中有一条待办，请及时办理。"};
//            String[] templateParamSet = {"1234"};
//            req.setTemplateParamSet(templateParamSet);
            String[] phoneNumberSet = {"17854217400"};
            req.setPhoneNumberSet(phoneNumberSet);
            String sessionContext = "";
            req.setSessionContext(sessionContext);
            String extendCode = "";
            req.setExtendCode(extendCode);
            String senderid = "";
            req.setSenderId(senderid);

            SendSmsResponse res = client.SendSms(req);

            System.out.println(SendSmsResponse.toJsonString(res));

        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
    }

    /**
     * 对传入的 List 进行快速排序
     *
     * @param list 待排序的 List，元素需实现 Comparable 接口
     * @param <T>  实现了 Comparable 接口的泛型类型
     */
    public static <T extends Comparable<T>> void quickSort(List<T> list) {
        if (list == null || list.size() <= 1) {
            return;
        }
        // 调用合并后的快速排序方法
        quickSortSingleMethod(list, 0, list.size() - 1);
    }

    /**
     * 合并后的快速排序方法，包含递归、分区和交换操作
     *
     * @param list 待排序的 List
     * @param low  左边界
     * @param high 右边界
     * @param <T>  实现了 Comparable 接口的泛型类型
     */
    private static <T extends Comparable<T>> void quickSortSingleMethod(List<T> list, int low, int high) {
        if (low < high) {
            T pivot = list.get(high);
            int i = low - 1;
            for (int j = low; j < high; j++) {
                if (list.get(j).compareTo(pivot) <= 0) {
                    i++;
                    T temp = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, temp);
                }
            }
            T temp = list.get(i + 1);
            list.set(i + 1, list.get(high));
            list.set(high, temp);
            int pivotIndex = i + 1;
            // 对基准值左边的元素进行递归排序
            quickSortSingleMethod(list, low, pivotIndex - 1);
            // 对基准值右边的元素进行递归排序
            quickSortSingleMethod(list, pivotIndex + 1, high);
        }
    }
}
